#include<stdio.h>
#include<stdlib.h>

void display(int *array,int n);
int* extend(int *array,int n,int *pn);
int* insert(int *array,int n);
int* remove_list(int *array,int n,int *pn);
int* pop(int *array,int n,int *pn,int *rt);
void index_list(int *array,int n,int *rt);
void count(int *array,int n,int *rt);
void sort(int *array,int n,int a);
int* append(int *array,int n);
     
int main(void)
{
  int ch=1,x,i,*array,n=0,*pn,*rt;
  array=(int *)malloc(sizeof(int));
  pn=(int *)malloc(sizeof(int));
  rt=(int *)malloc(sizeof(int));
  *pn=0;
  *rt=0;
  while(ch!=4)
  {
    system("cls");
    printf(">>>Main Menu<<<\n1.Create a list(this will overwrite the previous list)\n2.Implement a function\n3.Display the list\n4.Exit\nEnter your choice:");
    scanf("%d",&ch);
    if(ch==1)
    {  printf("1 list loaded");
       getch();
    }
    else if(ch==2)
    {
      while(ch!=11)
      {
        system("cls");
        printf(">>>Function Menu<<<\n1.list.append(x)\n2.list.extend(L)\n3.list.insert(i,x)\n4.list.remove(x)\n5.list.pop([i])\n6.list.index(x)\n7.list.count(x)\n8.list.sort()\n9.list.reverse()\n10.list.display()\n11.Return to previous menu\nEnter your choice:");
        scanf("%d",&ch);
        switch(ch)
        {
          case 1:
               n++;
               array=append(array,n);
            break;
          case 2:array=extend(array,n,pn);
               n=*pn;
          break;
          case 3:
               n++;
               array=insert(array,n);
            break;
          case 4:n--;
            array=remove_list(array,n,pn);
            n=*pn;
            break;
          case 5:
               n--;
               array=pop(array,n,pn,rt);
               n=*pn;
               break;
          case 6:index_list(array,n,rt);
            break;
          case 7:count(array,n,rt);
            break;
          case 8:sort(array,n,0);
               break;
          case 9:sort(array,n,1);
               break;
          case 10:display(array,n);break;
          case 11:break;
          default:printf("Invalid input\n");
        }    
      }
    }
    else if(ch==3)
         display(array,n);
  }
  return 0;
}

void display(int *array,int n)
{
     int i=0;
     printf("[");
     for(i=0;i<n;i++)
       printf("%d,",array[i]);
     printf("\b]\n");
     printf("n=%d\n",n);
     getch();
}
int* extend(int *array,int n,int *pn)
{
     int a[10],i,m;
     printf("Enter number of elements in list2:");
     scanf("%d",&m);
     for(i=0;i<m;i++)
     {
       printf("Enter element %d:",i+1);
       scanf("%d",&a[i]);
     }
     array=(int *)realloc(array,sizeof(int)*(n+m));
     *pn=n+m;
     for(i=0;i<m;i++)
       array[i+n]=a[i];
     return array;
}
int* insert(int *array,int n)
{
     int temp=0,i,x,j;
     array=(int *)realloc(array,sizeof(int)*n);
     printf("Enter i:");
     scanf("%d",&i);
     printf("Enter x:");
     scanf("%d",&x);
     j=n-2;
     while(j!=i-2)
     {
       array[j+1]=array[j];
       j--;
     }
     array[i-1]=x;
     return array;
}
int* remove_list(int *array,int n,int *pn)
{
     int i=0,data,temp=0;
     printf("Enter data:");
     scanf("%d",&data);
     while(i<n+1)
     {
       if(data==array[i])
       {
         temp=1;
         break;
       }
       i++;
     }
     if(temp==0)
     {
       printf("No such element in the list.\n");
       getch();
       *pn=n+1;
       return array;
     }
     i=0;
     while(i<n)
     {
       if(array[i]==data)
       {
         temp=array[i+1];
         array[i+1]=array[i];
         array[i]=temp;
       }
       i++;
     }  
     array=(int *)realloc(array,sizeof(int)*n);
     *pn=n;
     return array;
}
int* pop(int *array,int n,int *pn,int *rt)
{
     int i,j=0,temp=0;
     printf("Enter i:");
     scanf("%d",&i);
     if(n==0)
     {
       pritnf("List is already empty.\n");
       getch();
       *pn=n+1;
       return array;
     }
     if(i<-1 || i>n)
     {
       printf("Invalid input\n");
       *pn=n+1;
       getch();
       return array;
     }
     else if(i==-1)
     {
       *rt=array[n];
       printf("%d returned",*rt);
       array=(int *)realloc(array,sizeof(int)*n);
       getch();
       *pn=n;
       return array;
     }
     *rt=array[i];
     printf("%d returned",*rt);
     j=i;
     while(j<n+1)
     {
       temp=array[j+1];
       array[j+1]=array[j];
       array[j]=temp;
       j++;
     }
     
     array=(int *)realloc(array,sizeof(int)*n);
     getch();
     *pn=n;
     return array;
}
void index_list(int *array,int n,int *rt)
{
     int data,i=0;
     printf("Enter data:");
     scanf("%d",&data);
     for(i=0;i<n;i++)
       if(array[i]==data)
         break;
     if(i<n)
     {
       *rt=i;
       printf("%d returned",*rt);
     }
     else
       printf("Data not present");
     getch();
}
void count(int *array,int n,int *rt)
{
  int c=0,data,i;
  printf("Enter data:");
  scanf("%d",&data);
  for(i=0;i<n;i++)
    if(array[i]==data)
      c++;
  *rt=c;
  printf("%d returned",*rt);
  getch();
}
void sort(int *array,int n,int a)
{
     int i=0,j=0,temp;
     for(i=0;i<n;i++)
       for(j=0;j<=i;j++)
         if(array[j]>array[i] && a==0)
         {
           temp=array[i];
           array[i]=array[j];
           array[j]=temp;
         }
         else if(array[j]<array[i] && a==1)
         {
           temp=array[i];
           array[i]=array[j];
           array[j]=temp;
         }
     printf("Array sorted\n");
     getch();
}
int* append(int *array,int n)
{
     array=(int *)realloc(array,sizeof(int)*n);
     printf("Enter data:");
     scanf("%d",&array[n-1]);
     return array;
}
